<?php

namespace app\web\service;

use app\common\model\commonModel;
use app\common\model\trResource;
use app\common\model\trSystemSet;
use app\common\util\ErrorCode;
use app\common\util\Tools;

/**
 * Class Auth
 * @package app\web\service
 * 首页数据服务类
 */
class HomeService
{
    private $returnData;

    public function __construct($data)
    {
        $this->returnData = $data;
    }

    public function getSetting(){
        $wxAppTitleSetting = trSystemSet::getValue("wxAppTitleSetting");
        $data["mainColor"] = isset($wxAppTitleSetting["mainColor"]) ? $wxAppTitleSetting["mainColor"] : '#19C5F4';
        $data["navbarShowMode"] = isset($wxAppTitleSetting["navbarShowMode"]) ? $wxAppTitleSetting["navbarShowMode"] : 'FOUR_ITEM_LINE';
        $data["wxappTitle"] = isset($wxAppTitleSetting["wxappTitle"]) ? $wxAppTitleSetting["wxappTitle"] : '跨境租车';
        $wxAppSlogan = trSystemSet::getValue("wxAppSlogan");
        $data["content"] = isset($wxAppSlogan["content"]) ? $wxAppSlogan["content"] : '九龙, 机场, 迪士尼直达';
        return $data;
    }

    public function getInitialize(){
        $wxAppTitleSetting = trSystemSet::getValue("wxAppTitleSetting");
        $data["mainColor"] = isset($wxAppTitleSetting["mainColor"]) ? $wxAppTitleSetting["mainColor"] : '#19C5F4';
        $data["navbarShowMode"] = isset($wxAppTitleSetting["navbarShowMode"]) ? $wxAppTitleSetting["navbarShowMode"] : 'FOUR_ITEM_LINE';
        $data["wxappTitle"] = isset($wxAppTitleSetting["wxappTitle"]) ? $wxAppTitleSetting["wxappTitle"] : '跨境租车';
        return $data;
    }

    // 小程序首页的banner
    public function getIndexBanner(){
        $data = commonModel::getFields(bannerTable, "title,icon,url", ["status" => 1, "mall_id" => request()->mallData["id"]], false, false);
        foreach ($data as $k => $item){
            $data[$k]["iconUrl"] = tome_dia(trResource::getValue("url", ["id" => $item["icon"]]));
        }
        return $data;
    }

    // 小程序首页的菜单
    public function getLineMenuInfo(){
        $data = commonModel::getList(lineMenuTable, "title,icon,url", ["status" => 1, "mall_id" => request()->mallData["id"]], 0, 0, "sort desc,id asc", false);
        if(!empty($data)){
            foreach ($data as $k => $item){
                if(isset($item["icon"])){
                    $data[$k]["iconUrl"] = tome_dia(trResource::getValue("url", ["id" => $item["icon"]]));
                }
            }
            return $data;
        }
        return [];
    }

    public function getAddress($params){
        if (!empty($params['latitude']) && !empty($params["longitude"])) {
            $key = trSystemSet::getValue("MapSetting");
            if(empty($key)){
                $key = ["TCMapKey" => "FMRBZ-2REKF-5VJJM-NW664-LTAK6-SMBEI"];
            }
            $url = "https://apis.map.qq.com/ws/geocoder/v1/?key={$key["TCMapKey"]}";
            $result = Tools::curl($url."&location={$params['latitude']},{$params["longitude"]}");
            $res = json_decode($result, true);
            if(!isset($res["result"])){
                $res["result"] = '';
            }
            $this->returnData['data'] = $res["result"];
        }else{
            $this->returnData['data'] = '';
        }
        return $this->returnData;
    }
}